﻿import { getPageStudent, deleteStudent } from '@/services/studentService'
import {
  ActionType,
  PageContainer,
  ProColumns,
  ProTable,
} from '@ant-design/pro-components';
import { Button, Divider, message, Popconfirm, Space, Typography } from 'antd';
import React, { useEffect, useRef, useState } from 'react';
import CreateModal from './components/CreateModal';
import UpdateModal from './components/UpdateModal';
import { STATUS_CODE, File_STATUS_NAME } from '@/constants/index';


const ${modelName}Page: React.FC<unknown> = () => {
  const [createModalVisible, setCreateModalVisible] = useState<boolean>(false);
  const [updateModalVisible, setUpdateModalVisible] = useState<boolean>(false);
  const [updateData, setUpdateData] = useState<DictionaryType.Dictionary>(
    {} as DictionaryType.Dictionary,
  );
  const actionRef = useRef<ActionType>();

  useEffect(() => {
  }, [])

  const doDelete = async (student: ${modelName}Type.${modelName}Response) => {
    const hide = message.loading('正在删除');
    if (!student?.Id) {
      return;
    }
    try {
      await deleteStudent({
        id: student.Id,
      });
      message.success('操作成功');
      actionRef.current?.reload();
    } catch (e: any) {
      message.error('操作失败，' + e.message);
    } finally {
      hide();
    }
  };

  const columns: ProColumns<${modelName}Type.${modelName}Response>[] = [
    ${for(field in tableSchema.FieldList)}
    {
       title: $if(field.FieldName==primaryKey)'id'${end}${else}'${field.Comment}'${end}
    }
    ${end}
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => (
        <Space split={<Divider type="vertical" />}>
          <Popconfirm
            title="您确定要删除么？"
            onConfirm={() => doDelete(record)}
            okText="确认"
            cancelText="取消"
          >
            <Typography.Link type="danger">删除</Typography.Link>
          </Popconfirm>
        </Space>
      ),
    },
  ];

  return (<PageContainer>
    <ProTable<${modelName}Type.${modelName}Response>
      headerTitle="${tableComment}"
      actionRef={actionRef}
      rowKey="id"
      search={{
        labelWidth: 'auto',
        defaultCollapsed: false,
      }}
      toolBarRender={() => [
        <Button
          key="1"
          type="primary"
          onClick={() => setCreateModalVisible(true)}
        >
          新建
        </Button>,
      ]}
      request={async (params, sorter) => {
        const searchParams: PageRequest = {
          pageSize: params.pageSize,
          current: params.current
        };

        let sortQuery = "", filterQuery = "";
        // eslint-disable-next-line guard-for-in
        for (const key in sorter) {
          sortQuery += `${key}|${sorter[key]}`;
        }
        searchParams['@sort'] = sortQuery;

        for (const key in params) {
          if (key !== "pageSize" && key !== "current") {
            filterQuery += `contains(${key},${params[key]})&`;
            params[key] = '';
          }
        }
        searchParams['@filter'] = filterQuery;
        searchParams['@relationships'] = 'AddUser,UpdateUser'

        const { Items, StatusCode } = await getPage${modelName}(searchParams);
        return {
          data: Items[0]?.Records || [],
          success: StatusCode === STATUS_CODE.ok,
          total: Items[0].Total,
        } as any;
      }}
      columns={columns}
    />
  </PageContainer>);
};

export default ${modelName}Page;
